﻿// See https://aka.ms/new-console-template for more information

using MQTTDebugTool.Utils;
using NCrontab;

var client = await MQTTClientManager.ConnectionAsync();
if (client != null)
{
    // while(true)
    // {
    //     Console.WriteLine("#############################################");
    //     Console.Write("topic: ");
    //     var topic = Console.ReadLine() ?? "";
    //
    //     Console.Write("Message: ");
    //     var message = Console.ReadLine() ?? "";
    //     
    //     await client.PublishMessageAsync(topic, message);
    // }

    string topic = "test";
    
    // client.Subscribe(topic, (message) =>
    // {
    //     Console.WriteLine($"Received topic: {topic}");
    //     Console.WriteLine($"Received message: {message} ");
    // });
    
    // var delayTask = new DelayTask(5000, 10, (args) =>
    // {
    //     Console.WriteLine( $"Publishing message to topic: {topic}, time:{args.LastExecuteTime}" );
    //     client.PublishMessage(topic: topic, "Hello World");
    // }).Start();
    
    var delayTask = new ScheduledTask("* * * * *", (args) =>
    {
        Console.WriteLine( $"Publishing message to topic: {topic}, time:{args.LastExecuteTime}" );
        client.PublishMessage(topic: topic, "Hello World");
    }).Start();
    
    Console.ReadLine();
}


else
{
    Console.WriteLine("Failed to connect to MQTT Broker");
    return;
}

